﻿using System;
using System.Windows.Forms;
using DC2010.Objects.Data;
using DC2010.Objects.Structures;
using DC2010.Tools;
using DC2010;

namespace DC2010_EditorInfo
{
    public partial class SpritesForm : Form
    {
        public SpriteInfo AnimationInfo { get; set; }
        public string OriginalId { get; set; }


        public SpritesForm()
        {
            InitializeComponent();

            AnimationInfo = new SpriteInfo();
        }

        private void EffectsForm_Load(object sender, EventArgs e)
        {
            InitializeLB();
        }

        private void InitializeLB()
        {
            imageListBox1.Items.Clear();

            foreach (SpriteInfo ei in Root.I.SpriteSystem.DictInfo.Values)
            {

                ImageListBoxItem ilbi = new ImageListBoxItem(ei.Id, Properties.Resources.Animations);
                imageListBox1.Items.Add(ilbi);
            }
        }

        private void crystalButton1_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(OriginalId))
            {
                //we are updating
                GatherInfo(AnimationInfo);
                Root.I.SpriteSystem.SaveInfo();
                InitializeLB();
            }
            else
            {
                //creating new
                GatherInfo(AnimationInfo);
                Root.I.SpriteSystem.AddInfo(AnimationInfo);
                Root.I.SpriteSystem.SaveInfo();
                InitializeLB();
            }
        }

        private void GatherInfo(SpriteInfo ei)
        {
            ei.Id = IdTB.Text;
            ei.ShortName = ShortTB.Text;
            ei.DivinePoints = DivinePointsTB.Text.ToInt();

            ei.Delay = HPTB.Text.ToInt();
            ei.TileWidth = textBox1.Text.ToInt();
            ei.TileHeight = textBox2.Text.ToInt();
            ei.Path = PathTB.Text;
            ei.IsAnimated = IsAnimatedCB.Checked;
        }

        private void FillWithObject()
        {
            if (AnimationInfo != null)
            {
                OriginalId = AnimationInfo.Id;
                IdTB.Text = AnimationInfo.Id;
                ShortTB.Text = AnimationInfo.ShortName;
                DivinePointsTB.Text = AnimationInfo.DivinePoints.ToString();

                HPTB.Text = AnimationInfo.Delay.ToString();
                textBox1.Text = AnimationInfo.TileWidth.ToString();
                textBox2.Text = AnimationInfo.TileHeight.ToString();
                PathTB.Text = AnimationInfo.Path;
                IsAnimatedCB.Checked = AnimationInfo.IsAnimated;
            }
        }

        private void imageListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string s = ((ImageListBoxItem)imageListBox1.SelectedItem).Text;
            AnimationInfo = Root.I.SpriteSystem.GetInfo(s);
            FillWithObject();
        }

        private void crystalButton2_Click(object sender, EventArgs e)
        {
            AnimationInfo = new SpriteInfo();
            OriginalId = "";
        }
    }
}
